<template>
  <div>
    <h2>添加商品</h2>
    名称<input type="text" v-model="name">
    价格<input type="number" v-model="price">
    图片<input type="file" id="img">
    分类<select v-model="cate_id">
      <option v-for="i in cates" :value="i.id">{{i.name}}</option>

    </select>
    <button @click="sub">添加</button>
  </div>
</template>

<script>
export default {
  data(){
    return{
      'name':'',
      'price':'',
      'cate_id':0,
      'cates':[]
    }
  },
  mounted(){
    this.axios({
      url:'http://127.0.0.1:8000/app01/cate/',
      method:'get',
    }).then(res=>{
      this.cates=res.data.data;
    })
  },
  methods:{
    sub:function(){
      let img=document.getElementById('img').files[0];

      let formdata=new FormData();

      formdata.append('name',this.name);
      formdata.append('price',this.price);
      formdata.append('img',img);
      formdata.append('cate_id',this.cate_id);

      this.axios({
        'url':'http://127.0.0.1:8000/app01/goods/',
        'method':'post',
        'data':formdata,
      }).then(res=>{
        alert(res.data.msg);
      })
    }
  }
}
</script>

<style>

</style>
